#include "BufferShrink.h"
#include "Common.h"

BufferShrink::BufferShrink(boost::beast::flat_buffer &buffer)
	: buffer_(buffer), count_(0)
{

}

void BufferShrink::check(size_t size)
{
	if (size == 0) {
		return;
	}

	size_t cap = buffer_.capacity();
	if (cap > kCheckSize && cap > kMultiple * size) {
		count_++;
		if (count_ > kMaxCount) {
			buffer_.shrink_to_fit();
			size_t newCap = buffer_.capacity();
			LOG(INFO) << "buffer shrink to fit, old:" << cap << ", new:" << newCap;
			count_ = 0;
		}
	}
	else {
		count_ = 0;
	}
}

